import axios from "axios";
import { Message } from "element-ui";
import { getStore, setStore } from "./storage";
import router from "../router";

// const http = axios.create({
// 	//通用请求的地址前缀
// 	baseURL: '',
// 	timeout: 10000, //超时时间
// })

let base = '/rc';

axios.defaults.timeout = 15000;

//添加请求拦截器
axios.interceptors.request.use(function(config) {
	// 在发送请求之前做些什么
	return config;
}, function(error) {
	Message.error('请求出错');
	//对请求错误做些什么
	return Promise.reject(error);
})

//添加响应拦截器
axios.interceptors.response.use(function(response) {
	const data = response.data;
	// console.log(data);
	if(data.code == 2002){
		Message.warning("token失效，请重新登录！");
		router.push({path:'/login'})
		
	}
	return response;
}, function(error) {
	//对响应错误做点什么
	return Promise.reject(error)
})

/**
 * 发送登录get请求
 */
export const getRequest = (url,params) => {
	let accessToken = getStore('accessToken');
	return axios({
		method: 'get',
		url:`${base}${url}`,
		params:params,
		headers: {
			'Authorization': accessToken,
		}
	});
};

/**
 * 发送登录get请求
 */
export const getRequestEncode = (url,params) => {
	let accessToken = getStore('accessToken');
	return axios({
		method: 'get',
		url:`${base}${url}`,
		params:params,
		transformRequest: [function(data){
			let ret = '';
			for (let it in data){
				ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
			}
			ret = ret.substring(0,ret.length - 1);
			return ret;
		}],
		headers: {
			'Authorization': accessToken
		}
	});
};

/**
 * 发送未登录get请求
 */
export const getNoAuthRequest = (url,params) => {
	return axios({
		method: 'get',
		url: `${base}${url}`,
		params: params
	});
};

/**
 * 发送已登录 表单格式 post请求
 */
export const postRequest = (url,params) => {
	let accessToken = getStore('accessToken');
	return axios({
		method: 'post',
		url: `${base}${url}`,
		data: params,
		transformRequest: [function(data){
			let ret = '';
			for (let it in data){
				ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
			}
			ret = ret.substring(0,ret.length - 1);
			return ret;
		}],
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
			'Authorization': accessToken
		}
	});
};

/**
 * 发送已登录 body post请求
 */
export const postBodyRequest = (url,params) => {
	let accessToken = getStore('accessToken');
	return axios({
		method: 'post',
		url: `${base}${url}`,
		data: params,
		headers: {
			'Authorization': accessToken,
			'Content-Type': 'application/json;charset=UTF-8'
		}
	});
};

/**
 * 发送未登录 form post请求
 */
export const postNoAuthRequest = (url,params) => {
	return axios({
		method: 'post',
		url: `${base}${url}`,
		data: params,
		transformRequest: [function(data){
			let ret = '';
			for (let it in data){
				ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
			}
			ret = ret.substring(0,ret.length - 1);
			return ret;
		}],
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
		}
	});
};

/**
 * 发送未登录 body post请求
 */
export const postNoAuthBodyRequest = (url,params) => {
	return axios({
		method: 'post',
		url: `${base}${url}`,
		data: params,
		headers: {
			'Content-Type': 'application/json;charset=UTF-8'
		}
	});
};



